package com.ikun.common.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @ClassName GenerateCode
 * @Description: 代码生成
 * @CreateDate: 2024/4/3 13:54
 */
public class GenerateCode {
    public static void main(String[] args) {
//       generateCode("sys","com.ikun.admin.modules.sys",
//               "sys_log_operation");
//        generateCode("gen","com.ikun.admin.modules.gen","gen_datasource","gen_table","gen_field_type","gen_table_entity");
//        generateCode("scheduled","com.ikun.admin.modules.scheduled","scheduled_task","scheduled_task_log");
//     generateCode("sys","com.ikun.admin.modules.sys","sys_dict_area");

    }

    /**
     * 从数据库生成代码
     * @param tablePrefix  表前缀
     * @Param 包路徑
     * @param tableNames 表名 可变数组
     */
    public static void generateCode(String tablePrefix,String packagePath, String... tableNames) {
        AutoGenerator generator = new AutoGenerator();
        GlobalConfig gc = new GlobalConfig();
        // 生成代码的根目录
        gc.setOutputDir(System.getProperty("user.dir") + "\\ikun-admin\\src\\main\\java");
        gc.setOpen(false);
        // 主键策略
        gc.setIdType(IdType.ASSIGN_ID);
        gc.setServiceName("%sService");
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setSwagger2(true);
        generator.setGlobalConfig(gc);

        DataSourceConfig ds = new DataSourceConfig();
        ds.setUrl("jdbc:mysql://106.54.219.66:3306/ikun-framework?serverTimezone=UTC");
        ds.setUsername("root");
        ds.setPassword("Hl171298");
        ds.setDriverName("com.mysql.cj.jdbc.Driver");
        ds.setDbType(DbType.MYSQL);
        PackageConfig pc = new PackageConfig();
        // 包路径
        pc.setParent(packagePath);
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        generator.setPackageInfo(pc);
        StrategyConfig sc = new StrategyConfig();
        sc.setInclude(tableNames);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        sc.setCapitalMode(true);
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setSuperServiceClass("");
        sc.setEntityLombokModel(true);
        sc.setRestControllerStyle(true);
        sc.setEntityLombokModel(true);
        sc.setTablePrefix(tablePrefix);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(new TableFill("creator", FieldFill.INSERT));
        tableFills.add(new TableFill("create_date", FieldFill.INSERT));
        tableFills.add(new TableFill("updater", FieldFill.INSERT_UPDATE));
        tableFills.add(new TableFill("update_date", FieldFill.INSERT_UPDATE));
        sc.setTableFillList(tableFills);
        generator.setStrategy(sc);
        generator.setDataSource(ds);
        generator.execute();
    }




}
